package medical_application;
/** CSIS 343 Group 2 Course Project
 *  @authors Agnes Oliveira, Liane Heidemann, Daniel Schraufnagel, Matthew Lee
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;

public class LoginGUI extends JFrame{
	// variables necessary for constructing our LoginGUI
	private static final long serialVersionUID = 1L;
	static String strLogin, strPassword;
	JButton loginButton;
	JTextField loginTextField, passwordTextField;
	static final String LOGIN = "login", PASSWORD = "password";
	static char[] PASSWORD2 = {'p', 'a', 's','s', 'w', 'o', 'r', 'd'}, charPassword;
	JPasswordField passwordField;
	/**
	 * Constructor for the LoginGUI
	 */
	public LoginGUI(){
		this.setSize(455, 175);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setTitle("L O G I N");
		this.setFont(Main.f);
		JPanel loginPanel = new JPanel();
		loginPanel.setLayout(new GridBagLayout());
		
		JLabel loginLabel = new JLabel("Login: ");
		loginLabel.setFont(Main.f);
		addToPanel(loginPanel, loginLabel, 0, 0, 1, 1, GridBagConstraints.EAST);
		
		loginTextField = new JTextField(15);
		loginTextField.setFont(Main.f);
		addToPanel(loginPanel, loginTextField, 1, 0, 1, 1, GridBagConstraints.WEST);
		
		JLabel passLabel = new JLabel("Password: ");
		passLabel.setFont(Main.f);
		addToPanel(loginPanel, passLabel, 0, 1, 1, 1, GridBagConstraints.EAST);
		
		passwordField = new JPasswordField(15);
		passwordField.setFont(Main.f);
		addToPanel(loginPanel, passwordField, 1, 1, 1, 1, GridBagConstraints.WEST);
		
		loginButton = new JButton("Confirm");
		loginButton.setFont(Main.f);
		addToPanel(loginPanel, loginButton, 3, 3, 1, 1, GridBagConstraints.SOUTHEAST);
		
		
		this.add(loginPanel);
		this.setVisible(true);
		this.setLocationRelativeTo(null);
		actionLogin();
	}
	
	/** This method adds components to the JPanel using the GridBagLayout Manager and GridBag constraints**/
	private void addToPanel(JPanel p, JComponent c, int x, int y, int width, int height, int align){
		GridBagConstraints gc = new GridBagConstraints();
		gc.gridx = x;
		gc.gridy = y;
		gc.gridwidth = width;
		gc.gridheight = height;
		gc.weightx = 100.0;
		gc.weighty = 100.0;
		gc.insets = new Insets(2, 2, 2, 2);
		gc.anchor = align;
		gc.fill = GridBagConstraints.NONE;
		p.add(c, gc);
	}//end of addToPanel()
	
	/**
	 * This method controls the login function, if successful, a new search window is instantiated
	 * @param a correct login / password
	 * @return a new search frame
	 */
	public void actionLogin(){
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				strLogin = loginTextField.getText();
				charPassword = passwordField.getPassword();		
				try{
					if(strLogin.equals(LOGIN) && Arrays.equals(charPassword, PASSWORD2)) {
						new SearchGUI();
						dispose();
					}		
					else {
						JOptionPane.showMessageDialog(null,"Wrong login and/or password", "W R O N G", JOptionPane.ERROR_MESSAGE);
						loginTextField.setText("");
						passwordField.setText("");
						loginTextField.requestFocus();
					}
				}catch(NullPointerException e){
					JOptionPane.showMessageDialog(null,"Missing Field(s)", "W R O N G", JOptionPane.ERROR_MESSAGE);
					loginTextField.setText("");
					passwordField.setText("");
					loginTextField.requestFocus();
				}	
				}
		});
	}//end of actionLogin
}//end of LoginGUI class

